/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package SYSGEST.br.Saida;

import SYSGEST.br.Produto.Produtos;
import SYSGEST.br.UndSaude.UndSaude;
import java.util.List;
import javax.swing.JOptionPane;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import sysgest.conexao.HibernateUtil;

/**
 *
 * @author Mesopotanea
 */
public class SaidaDAO {

    private Session sessao;
    private Transaction transacao;

   public void salvar(Saida saida) {
        sessao = HibernateUtil.getSessionFactory().openSession();
        transacao = sessao.beginTransaction();
        sessao.save(saida);
        transacao.commit();
        sessao.close();
    }

    public void excluir(Saida saida) {
        sessao = HibernateUtil.getSessionFactory().openSession();
        transacao = sessao.beginTransaction();
        sessao.delete(saida);
        transacao.commit();
        sessao.close();
    }
    
    public List<Saida> listar() {
        sessao = HibernateUtil.getSessionFactory()
                .openSession();
        transacao = sessao.beginTransaction();
        Criteria criteria = sessao.createCriteria(Saida.class);
        List<Saida> saidasproduto = criteria.list();
        return saidasproduto;
    }

    public Produtos pesquisaCdBarra(String cbBarra) {
        sessao = HibernateUtil.getSessionFactory().openSession();
        transacao = sessao.beginTransaction();
        Produtos saida
                = (Produtos) sessao.createCriteria(Produtos.class).add(Restrictions.eq("codBarras", cbBarra)).uniqueResult();
        return saida;
    }

    public UndSaude pesquisaESF(String Codigo) {
        sessao = HibernateUtil.getSessionFactory().openSession();
        transacao = sessao.beginTransaction();
        UndSaude saida
                = (UndSaude) sessao.createCriteria(UndSaude.class).add(Restrictions.eq("codpsf", Codigo)).uniqueResult();
        return saida
                ;
    }
    public Saida pesquisacodigo(int Codigo) {
        sessao = HibernateUtil.getSessionFactory().openSession();
        transacao = sessao.beginTransaction();

        Saida prod
                = (Saida) sessao.createCriteria(Saida.class).
                add(Restrictions.eq("ID", Codigo)).
                uniqueResult();
        sessao.close();
        return prod;

    }
}
